<template>
  <div>
    <div class="category">
      <el-tree
        :data="data"
        node-key="id"
        :props="defaultProps"
        :default-expand-all="true"
      >
        <template #default="{ node, data }">
          <span class="custom-tree-node">
            <div>
              <el-image
                v-if="data.picUrl && data.level == 2"
                :src="data.picUrl"
              ></el-image>
              <span>{{ node.label }}</span>
              <span style="margin-left: 8px">
                <el-tag v-if="data.level == 0" type="danger">一级</el-tag>
                <el-tag v-if="data.level == 1" type="success">二级</el-tag>
                <el-tag v-if="data.level == 2" type="success">三级</el-tag>
              </span>
            </div>
            <span class="concatenation">
              <el-button
                round
                @click.stop="append(1, data)"
                type="primary"
                size="small"
                v-if="node.level != 4"
                v-permission="'product:category:create'"
                >添加</el-button
              >
              <el-button
                round
                @click="editor(node, data)"
                type="warning"
                size="small"
                v-else-if="node.level != 1"
                >编辑</el-button
              >
              <el-button
                round
                @click="remove(node, data)"
                type="danger"
                size="small"
                v-if="!data.childrenList"
                >删除</el-button
              >
            </span>
          </span>
        </template>
      </el-tree>
      <!-- 添加对话框 -->
      <el-dialog
        v-model="adds"
        :title="title"
        width="30%"
        :before-close="handleClose"
      >
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
        >
          <el-form-item label="类目" prop="title">
            <el-input
              v-model.trim="ruleForm.title"
              type="text"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="类目图片" prop="picUrl">
            <el-upload
              class="avatar-uploader"
              :action="addback + '/upload/admin'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              :headers="token"
            >
              <img
                v-if="ruleForm.picUrl"
                :src="ruleForm.picUrl"
                class="avatar"
                style="width: 80px; height: 80px"
              />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="adds = false">取消</el-button>
            <el-button type="primary" @click="definiteness">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import http from "@/http";
import host from "@/http";
import cookin from "js-cookie";
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
      id: "", //类目ID
      title: "添加", //类目标题
      level: "", //类目等级
      parentId: "", //父类目ID
      adds: false, //对话框
      defaultProps: {
        children: "childrenList",
        label: "title",
      },
      //创建
      ruleForm: {
        title: "",
        parentId: "",
        iconUrl: "",
        picUrl: "",
      },
      paging: {
        page: 1, //默认第一页
        limit: 10, //显示几页
        total: null,
      },
      data: [],
    };
  },
  created() {
    this.category();
    this.token = {
      AdminToken: cookin.get("token"),
    };
  },
  computed: {
    addback() {
      return host.host;
    },
  },
  methods: {
    category() {
      let data = {
        _gp: "admin.category",
        _mt: "categoryTree",
      };
      http.post(data).then((res) => {
        if (200 == res.errno) {
          this.data = [{ id: 0, title: "全部类目", childrenList: res.data }];
        }
      });
    },
    //添加
    append(index, row) {
      this.adds = true;
      this.ruleForm.parentId = row.id;
    },
    definiteness() {
      if (this.title == "添加") {
        let data = {
          _gp: "admin.category",
          _mt: "create",
          ...this.ruleForm,
        };
        http.post(data).then((res) => {
          if (200 == res.errno) {
            ElMessage.success("添加成功！");
            this.ruleForm = this.$options.data().ruleForm;
            this.category();
            this.adds = false;
          }
        });
      } else {
        let datas = {
          _gp: "admin.category",
          _mt: "update",
          id: this.id,
          ...this.ruleForm,
        };
        http.post(datas).then((res) => {
          if (200 == res.errno) {
            ElMessage.success("编辑成功！");
            this.adds = false;
            this.category();
            this.ruleForm = this.$options.data().ruleForm;
          }
        });
      }
    },
    //图片上传回调
    handleAvatarSuccess(res) {
      if (200 == res.errno) {
        this.ruleForm.picUrl = res.url;
      }
    },
    //编辑
    editor(index, row) {
      this.id = row.id;
      this.adds = true;
      this.title = "编辑";
      Object.assign(this.ruleForm, row);
    },
    //删除
    remove(index, row) {
      ElMessageBox.confirm("是否删除此数据", {
        confirmButtonText: "OK",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let deletes = {
            _gp: "admin.category",
            _mt: "delete",
            id: row.id,
          };
          http.post(deletes).then((res) => {
            if (200 == res.errno) {
              this.category();
            }
          });
          ElMessage({
            type: "success",
            message: "删除成功",
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消成功",
          });
        });
    },
  },
};
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style lang="less" scoped>
.category {
  width: 98%;
  margin: auto;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;

  .el-image {
    height: 20px;
    width: 20px;
    vertical-align: middle;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  text-align: center;
  border: 1px dashed #ccc;
}
</style>
